package com.sc.study.foundation.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.sc.common.annotation.Excel;
import com.sc.common.core.domain.entity.SysUser;
import com.sc.study.foundation.domain.ScSubjectChapter;
import com.sc.study.foundation.domain.ScSubjectTeacher;
import com.sc.study.foundation.service.IScSubjectTeacherService;
import com.sc.study.student.domain.ScStudent;
import com.sc.system.service.ISysConfigService;
import com.sc.system.service.ISysUserService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.foundation.domain.ScSubject;
import com.sc.study.foundation.service.IScSubjectService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程Controller
 *
 * @author sc
 * @date 2022-02-27
 */
@RestController
@RequestMapping("/study/subject")
public class ScSubjectController extends BaseController {
    @Autowired
    private IScSubjectService scSubjectService;

    @Autowired
    private IScSubjectTeacherService scSubjectTeacherService;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('study:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScSubject scSubject) {
        startPage();
        List<ScSubject> list = scSubjectService.selectScSubjectList(scSubject);
        return getDataTable(list);
    }


    /**
     * 查询所有： 下拉展示
     * 带出属性 选择题的题目对象
     *
     * @return
     */
    @GetMapping("/listsubjectIncludeQuestions")
    public AjaxResult listsubjectIncludeQuestion() {
        return AjaxResult.success(scSubjectService.listsubjectIncludeQuestions());
    }


    /**
     * 查询课程列表包含老师和年级
     */
    @PreAuthorize("@ss.hasPermi('study:subject:listsubjectteacher')")
    @GetMapping("/listSubjectTeacher")
    public TableDataInfo listSubjectTeacher(ScSubject scSubject) {
        return getDataTable(scSubjectService.selectSubjectIncludeGradeAndUsers(scSubject));
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('study:subject:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScSubject scSubject) {
        List<ScSubject> list = scSubjectService.selectScSubjectList(scSubject);
        ExcelUtil<ScSubject> util = new ExcelUtil<ScSubject>(ScSubject.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:subject:query')")
    @GetMapping(value = "/{subid}")
    public AjaxResult getInfo(@PathVariable("subid") Long subid) {
        return AjaxResult.success(scSubjectService.selectScSubjectBySubid(subid));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('study:subject:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScSubject scSubject) {
        return toAjax(scSubjectService.insertScSubject(scSubject));
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('study:subject:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScSubject scSubject) {
        return toAjax(scSubjectService.updateScSubject(scSubject));
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('study:subject:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{subids}")
    public AjaxResult remove(@PathVariable Long[] subids) {
        return toAjax(scSubjectService.deleteScSubjectBySubids(subids));
    }

    /**
     * 下载课程列表模板（在课程列表中的模板）
     */
    @PreAuthorize("@ss.hasPermi('study:subject:importExcelModel')")
    @Log(title = "课程", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcelModel")
    public ResponseEntity<byte[]> importExcelModel(HttpServletResponse response) throws IOException {
        //创建一个excel
        Workbook workbook = new XSSFWorkbook();
        //创建sheet页
        Sheet sheet = workbook.createSheet();
        Row rowTitle = sheet.createRow(0);

        //构建sheet数据标题
        Cell cellNameTile = rowTitle.createCell(0);
        cellNameTile.setCellValue("课程");

        Row rowTitle2 = sheet.createRow(1);
        //构建sheet数据标题
        Cell cellName3Tile = rowTitle2.createCell(0);
        cellName3Tile.setCellValue("课程名称");
        Cell cellName4Tile = rowTitle2.createCell(1);
        cellName4Tile.setCellValue("年级");
        Cell cellName5Tile = rowTitle2.createCell(2);
        cellName5Tile.setCellValue("每次消耗课时");
        Cell cellName6Tile = rowTitle2.createCell(3);
        cellName6Tile.setCellValue("每课时费用");
        Cell cellName7Tile = rowTitle2.createCell(4);
        cellName7Tile.setCellValue("老师每课时费用");
        Cell cellName8Tile = rowTitle2.createCell(5);
        cellName8Tile.setCellValue("每周多少次课");
        Cell cellName9Tile = rowTitle2.createCell(6);
        cellName9Tile.setCellValue("不连续多少次");


        Row rowTitle3 = sheet.createRow(3);
        //构建sheet数据标题
        Cell cellName10Tile = rowTitle3.createCell(0);
        cellName10Tile.setCellValue("课程章节");

        Row rowTitle4 = sheet.createRow(4);
        //构建sheet数据标题
        Cell cellName11Tile = rowTitle4.createCell(0);
        cellName11Tile.setCellValue("章节名称");
        Cell cellName12Tile = rowTitle4.createCell(1);
        cellName12Tile.setCellValue("顺序");
        Cell cellName15Tile = rowTitle4.createCell(2);
        cellName15Tile.setCellValue("课时");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        workbook.write(bos);

        HttpHeaders headers = new HttpHeaders();
        //响应头的的流名称
        headers.setContentDispositionFormData(
                "attachment",
                new String(("课程数据-1111.xlsx").getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        //响应头出数据的格式为STREAM（流）
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bos.toByteArray(), headers, HttpStatus.OK);
        return responseEntity;
    }

    /**
     * 导入课程列表
     * @param file excel
     * @throws IOException
     */
    @PreAuthorize("@ss.hasPermi('study:subject:importExcel')")
    @Log(title = "课程", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcel")
    public void importExcel(MultipartFile file) throws IOException {
        scSubjectService.importExcel(file);
    }

}
